package com.llkj.common.observer;

import com.llkj.common.exception.ApiException;
import com.llkj.common.mvp.IListView;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/8
 * desc  :
 */
public abstract class BaseListObserver<T> extends BaseObserver<T> {

    private IListView mIView;
    private boolean isRefresh;
    private int pageIndex;

    public BaseListObserver(IListView mIView, boolean isRefresh, int pageIndex) {
        this.mIView = mIView;
        this.isRefresh = isRefresh;
        this.pageIndex = pageIndex;
    }

    @Override
    public void doOnStart() {
        if (isRefresh) {
            mIView.showRefreshLoading();
        } else {
            if (pageIndex == 1) {
                mIView.showLoadingDialog("");
            }
        }
    }

    @Override
    public void doOnCompleted() {
        if (pageIndex == 1) {
            mIView.hideLoadingDialog();
        }
        mIView.finishRefresh();
    }

    @Override
    public void doOnError(ApiException e) {
        if (pageIndex == 1) {
            mIView.showErrorException(e);
            if (isRefresh) {
                mIView.finishRefresh();
            } else {
                mIView.hideLoadingDialog();
            }
        } else {
            mIView.loadMoreFail();
        }
    }
}
